/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 20.02.12
 * Time: 17:21
 * To change this template use File | Settings | File Templates.
 */

KeyBoardType = {
    WASD : "WASD",
    ARROWS : "ARROWS"
};
ArrowKeysEnums = {
    LEFT: 37,
    UP: 38,
    RIGHT: 39,
    DOWN: 40,
};

WASDEnums = {
    LEFT : 65,
    UP : 87,
    RIGHT : 68,
    DOWN : 83
};

ControlKeysEnums = {
    CTRL : 17,
    SPACE : 32
};

function keyDown(evt) {
    keys[evt.keyCode] = true;
}

function keyUp(evt) {
    keys[evt.keyCode] = false;
    keylock[evt.keyCode] = false;
}

function updateArrowKeys(player) {
    if (ControlKeysEnums.CTRL in keys && keys[ControlKeysEnums.CTRL]) { //ctrl
        alert("CTRL");
        canvasLostFocus();
    }

    if (player.assingedKeys == KeyBoardType.ARROWS){
        updateARROWS(player);
    }

    if (player.assingedKeys == KeyBoardType.WASD){
        updateWASD(player);

    }

}
function updateARROWS(player){

    if (ArrowKeysEnums.DOWN in keys && keys[ArrowKeysEnums.DOWN]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() + 1)) {
                player.moveY(1);
            }
        }
    }

    if (ArrowKeysEnums.LEFT in keys && keys[ArrowKeysEnums.LEFT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() - 1, player.getPosY())) {
                player.moveX(-1);
            }
        }
    }

    if (ArrowKeysEnums.RIGHT in keys && keys[ArrowKeysEnums.RIGHT]){

        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() + 1, player.getPosY())) {
                player.moveX(1);
            }
        }
    }

    if (ArrowKeysEnums.UP in keys && keys[ArrowKeysEnums.UP]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() - 1)) {
                player.moveY(-1);
            }
        }
    }
}

function updateWASD(player) {
    if (WASDEnums.DOWN in keys && keys[WASDEnums.DOWN]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() + 1)) {
                player.moveY(1);
            }
        }
    }

    if (WASDEnums.LEFT in keys && keys[WASDEnums.LEFT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() - 1, player.getPosY())) {
                player.moveX(-1);
            }
        }
    }

    if (WASDEnums.RIGHT in keys && keys[WASDEnums.RIGHT]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX() + 1, player.getPosY())) {
                player.moveX(1);
            }
        }
    }

    if (WASDEnums.UP in keys && keys[WASDEnums.UP]){
        if (!player.isMoving()){
            if (isPlaceFree(player.getPosX(), player.getPosY() - 1)) {
                player.moveY(-1);
            }
        }
    }
}
